package cn.yunhe.controller;

import cn.yunhe.model.User;
import cn.yunhe.service.impl.UserServiceImpl;
import com.github.pagehelper.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/free")
public class FreeController {
    @Resource
    private UserServiceImpl userService;

    @RequestMapping("/index")
    public ModelAndView test(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("index");
        return mv;
    }
    @RequestMapping("/test1")
    public User test1(){
        User user = new User();
        user.setUsername("牛儿");
        user.setPassword("000000");
        return user;
    }

    @RequestMapping("/getPage")
    public Map queryUsersByPage(int pageNum, int pageSize, User user) {
        Map map = new HashMap();
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);
        map.put("user", user);
        Page page = userService.queryLikeUsers(map);
        map.put("page", page);
        map.put("totalPage", page.getPages());
        List<User> list = page.getResult();
        map.put("result",list);
        return map;
    }
    @RequestMapping("/addUser")
    @ResponseBody
    public ModelAndView addUser(User user){
        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        userService.addUser(user);
        return mv;
    }
    @RequestMapping("/delUser")
    public ModelAndView delUser(User user){
        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        userService.delUser(user);
        return mv;
    }
    @RequestMapping("/findUser")
    public ModelAndView findUser(User user){
        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        User u = userService.findUser(user);
        mv.addObject("user",u);
        mv.setViewName("editUser");
        return mv;
    }

    @RequestMapping("/editUser")
    public ModelAndView editUser(User user){
        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        userService.editUser(user);
        return mv;
    }

    @RequestMapping("logins")
    public ModelAndView logins(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("login");
        return mv;
    }

   /* @RequestMapping("/login")
    public ModelAndView login(User user,HttpSession session){
        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
            User u = userService.login(user);
        if(null!=u){
            session.setAttribute("user",u);
            mv.setViewName("index");
        }else {
            mv.setViewName("logins");
        }
        return mv;
    }*/
    @RequestMapping("/reg")
    public ModelAndView reg(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("regist");
        return mv;
    }
    @RequestMapping("regist")
    public ModelAndView regist(User user){
        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        userService.addUser(user);
        mv.setViewName("login");
        return mv;
    }
    @RequestMapping("/admin")
    public ModelAndView adminPage(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("admin");
        return mv;
    }
    @RequestMapping("/user")
    public ModelAndView userPage(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("user");
        return mv;
    }

    public UserServiceImpl getUserService() {
        return userService;
    }

    public void setUserService(UserServiceImpl userService) {
        this.userService = userService;
    }
}
